define([
    'zepto',
    'underscore',
    'backbone',
    "text!templates/qpay/qPayResult.html",
    "wTools",
    "area",
    "validate"

], function ($, _, Backbone, template) {
    return Backbone.View.extend({
        $el: $main,
        el: $main0,
        template: _.template(template),
        events: {
            "tap #btn": "btn"
        },
        initialize: function () {
            $("#head_back").hide();

            var postData = {
                "token": wData.urlData.token
            }, that = this;
            if(wData.status){
                wAjaxForm("getTokenInfo", "", postData, function (result) {
                    if (result.success) {
                        that.render(wData);
                        wData.trxId = result.tokenInfo.trxId;
                        wData.userNo = result.tokenInfo.userNo;
                        wData.returnUrl = result.tokenInfo.returnUrl;
                        if(wData.returnUrl==''||wData.returnUrl==undefined||wData.returnUrl==null){
                            $('#btn').remove();
                        }
                        else{
                            var timeFiv = 5;
                            $("#resultMsg").html( timeFiv + "s后自动返回");
                            var IntervalNum = setInterval(function () {
                                timeFiv--;
                                if (timeFiv == 0) {
                                    clearInterval(IntervalNum);
                                    that.btn();
                                    //返回地址backURL;
                                } else {
                                    $("#resultMsg").html(timeFiv + "s后自动返回");
                                }
                            }, 1000);
                        }
                    }else{
                        wTips("请重试");
                    }
                },function() {
                    wData.errorMessage = "操作超时,请重试";
                    location.hash = "fail";
                });
            }
        },
        back: function () {
            history.back();
        },
        render: function (data) {
            var html = this.template(data);
            this.$el.html(html);
        },
        btn: function () {
            if(wData.isBindCard == '0'){
                location.href = wData.returnUrl;
                // location.href = wData.returnUrl+'?'+'status='+wData.status+'&'+'trxId='+wData.trxId+'&'+'merUserId='+wData.userNo+'&'+'bankName='+wData.cardInfo.cardName+'&'+'cardBegin='+wData.cardInfo.cardNo.substring(0,6)+'&'+'cardEnd='+wData.cardInfo.cardNo.substring(wData.cardInfo.cardNo.length-4);
            }
            else if(wData.isBindCard == '1'){
                location.href = wData.returnUrl;
                // location.href = wData.returnUrl+'?'+'status='+wData.status+'&'+'trxId='+wData.trxId+'&'+'merUserId='+wData.userNo;
            }
        },

        destroy: function () {
            this.undelegateEvents();
            this.$el.html("");
            delete this;
        }
    });
});